package com.bmps.springcloud.insuredmicroservice.config.rabbitmq;

import com.bmps.springcloud.insuredmicroservice.service.rabbitmq.production.AcceptConfirmCallback;
import com.bmps.springcloud.insuredmicroservice.service.rabbitmq.production.AcceptReturnCallback;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.validation.constraints.NotBlank;


/**
 * 消息队列配置
 *
 * @date: 2019/12/23  17:17
 **/
@Configuration
public class RabbitConfig {
    @Value("${customize.routing}")
    @NotBlank
    private String acceptRouting;
    @Value("${customize.queue}")
    @NotBlank
    private String acceptQueue;
    private final AcceptConfirmCallback acceptConfirmCallback;
    private final AcceptReturnCallback acceptReturnCallback;
    /**
     * 投保消息交换机的名字
     */
    public static final String EXCHANGE_bmps_ACCEPT = "exchange_bmps_accept";
    /**
     * 投保消息队列
     */
    public static final String QUEUE_bmps_ACCEPT = "queue_bmps_accept";
    /**
     * 投保消息路由键
     */
    //public static final String ROUTING_KEY_ACCEPT = "routing_key_accept";
    /**
     * 投保消息死信交换机
     */
    public static final String DLX_EXCHANGE_bmps_ACCEPT = "exchange_dlx_bmps_accept";

    /**
     * 投保消息死信队列
     */
    public static final String DLX_QUEUE_bmps_ACCEPT = "queue_dlx_bmps_accept";

    /**
     * 投保消息路由键
     */
    //public static final String DLX_ROUTING_KEY_ACCEPT = "routing_dlx_key_accept";
    public RabbitConfig(AcceptConfirmCallback acceptConfirmCallback, AcceptReturnCallback acceptReturnCallback) {
        this.acceptConfirmCallback = acceptConfirmCallback;
        this.acceptReturnCallback = acceptReturnCallback;
    }

    /**
     * 下面都是采用direct， 必须严格匹配exchange和queue
     * （springBoot2.2.2版本不用强制类型转换）
     * 投保消息交换机
     */
    @Bean("bmpsAcceptExchange")
    DirectExchange bmpsAcceptExchange() {
        return (DirectExchange) ExchangeBuilder.directExchange(EXCHANGE_bmps_ACCEPT).durable(true).build();

    }

    /**
     * 投保消息队列
     */
    @Bean("bmpsAcceptQueue")
    public Queue bmpsAcceptQueuePreprod() {
        return QueueBuilder.durable(acceptQueue).build();
    }

    /**
     * 交换机、队列、绑定
     */
    @Bean("bindingbmpsAcceptExchange")
    Binding bindingbmpsAcceptExchange(@Qualifier("bmpsAcceptQueue") Queue queue,
                                      @Qualifier("bmpsAcceptExchange") DirectExchange directExchange) {
        return BindingBuilder.bind(queue).to(directExchange).with(acceptRouting);
    }

    /**
     * 投保死信交换机
     */
    @Bean("bmpsDlxAcceptExchange")
    DirectExchange bmpsDlxAcceptExchange() {
        return (DirectExchange) ExchangeBuilder.directExchange(DLX_EXCHANGE_bmps_ACCEPT).durable(true).build();
    }

    /**
     * 投保死信队列
     */
    @Bean("bmpsDlxAcceptQueue")
    public Queue bmpsDlxAcceptQueue() {
        return QueueBuilder.durable(DLX_QUEUE_bmps_ACCEPT).build();
    }

    /**
     * 死信交换机、队列、绑定
     */
    @Bean("bindingDlxbmpsAcceptExchange")
    Binding bindingDlxbmpsAcceptExchange(@Qualifier("bmpsDlxAcceptQueue") Queue queue,
                                         @Qualifier("bmpsDlxAcceptExchange") DirectExchange directExchange) {
        return BindingBuilder.bind(queue).to(directExchange).with(acceptRouting);
    }

    @Bean
    public MessageConverter messageConvert() {
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory factory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(factory);
        rabbitTemplate.setConfirmCallback(acceptConfirmCallback);
        rabbitTemplate.setMandatory(true);
        rabbitTemplate.setMessageConverter(messageConvert());
        rabbitTemplate.setReturnCallback(acceptReturnCallback);
        //使用单独的发送连接，避免生产者由于各种原因阻塞而导致消费者同样阻塞
        rabbitTemplate.setUsePublisherConnection(true);
        return rabbitTemplate;
    }


}
